import {
  model
} from "@/utils/extendAPI"
const swipeCellBehaviors = require("@/behaviors/swipeCell")
import {
  getCartList,
  checkCart,
  checkAllCart,
  addToCart,
  deleteGood
} from "@/api/cart"

import {
  createStoreBindings
} from "mobx-miniprogram-bindings"
const computedBehavior = require('miniprogram-computed').behavior
import {
  store
} from "@/store/store"
Component({

  behaviors: [computedBehavior, swipeCellBehaviors],
  data: {
    carList: [],
    token: '',
    ischeckAllCart: false,
    description: "还没有商品，快去添加吧~",
    sum: 0,
    flag: false
  },
  computed: {
    // 商品选择改变全选状态
    ischeckAllCart(data) {
      // 返回false 表示没有数据,全部对比
      return data.carList.length !== 0 && data.carList.every((item) => {
        return item.isChecked === 1
      });
    },
    countM(data) {
      console.log("countM");
      let sum = 0
      data.carList.forEach((item) => {
        if (item.isChecked) {
          sum += item.price * item.count
        }
      })
      return sum
    }
  },
  methods: {
    onLoad() {
      this.storeBindings = createStoreBindings(this, {
        store,
        fields: ["token"]
      })
      this.storeBindings.updateStoreBindings()
    },

    async getCartData() {
      const {
        code,
        data
      } = await getCartList()
      if (code !== 200) {
        wx.showToast({
          title: '数据加载异常',
          icon: "error"
        })
        return
      }
      this.setData({
        carList: data,
        description: data.length === 0 && '还没有添加商品,快去添加吧~'
      })
    },
    // 登录/获取数据 提示信息
    showTipGetList() {
      if (!this.data.token) {
        this.setData({
          description: "您尚未登录,点击登录获取更多权益",
          carList: []
        })
        return
      }
      this.getCartData()

    },
    onShow() {
      this.showTipGetList()
      this.data.carList.forEach(element => {
        if (element.isChecked === 1) {
          this.setData({
            ischeckAllCart: true
          })
        }
      })
    },
    // 更新商品的购买状态   购买/不购买 的选中
    async updateChecked(e) {
      const param = (this.data.carList[e.target.dataset.index]);
      param.isChecked = param.isChecked ? 0 : 1
      await checkCart(param)
      this.getCartData()
    },
    // 全选按钮
    async AllChange() {
      this.setData({
        ischeckAllCart: !this.data.ischeckAllCart
      })
      await checkAllCart(this.data.ischeckAllCart ? 1 : 0)
      this.getCartData()
    },
    onUnload() {
      this.storeBindings.destroyStoreBindings()
    },
    // 修改购买数量
    async onChange(e) {
      console.log("this.data.flag"+this.data.flag);
      if (!this.data.flag) {
        this.setData({
          flag: true
        })
        const newValue = e.detail > 200 ? 200 : e.detail
        const oldValue = e.target.dataset.oldvalue > 200 ? 200 : e.target.dataset.oldvalue
        const {
          goodsid,
        } = e.target.dataset
        let disCount = newValue - oldValue
        if(disCount===0){
          this.setData({
            flag: false
          })
          return 
        }
        const res = await addToCart({
          goodsId: goodsid,
          count: disCount
        })
        console.log(res.code);
        if (res.code === 200) {
          this.setData({
            flag: false
          })
          this.getCartData()
        }
      }y
    },
    // 删除商品
    async removeGood(e) {
      const goodId = e.target.dataset.id
      const isRemove = await wx.model({
        title: '删除',
        content: '确定删除该商品吗?'
      })
      if (isRemove) {
        const res = await deleteGood(goodId)
        console.log(res);
        this.getCartData()
      }
    },
    // 跳转结算
    onClickButton(){
      wx.navigateTo({
        url: '/orderPayModule/pages/order/detail/detail?',
      })
    }
  }
})